3 * Hooks.php -- a tool for running hook functions
4 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * @author Evan Prodromou <evan@wikitravel.org>
27 * Because programmers assign to $wgHooks, we need to be very
28 * careful about its contents. So, there's a lot more error-checking
29 * in here than would normally be necessary.
31 function wfRunHooks($event, $args = null) {
34 $fname = 'wfRunHooks';
35 wfProfileIn( $fname );
37 if (!is_array($wgHooks)) {
38 wfDebugDieBacktrace("Global hooks array is not an array!\n");
39 wfProfileOut( $fname );
43 if (!array_key_exists($event, $wgHooks)) {
44 wfProfileOut( $fname );
48 if (!is_array($wgHooks[$event])) {
49 wfDebugDieBacktrace("Hooks array for event '$event' is not an array!\n");
50 wfProfileOut( $fname );
54 foreach ($wgHooks[$event] as $index => $hook) {
62 /* $hook can be: a function, an object, an array of $function and $data,
63 * an array of just a function, an array of object and method, or an
64 * array of object, method, and data.
67 if (is_array($hook)) {
68 if (count($hook) < 1) {
69 wfDebugDieBacktrace("Empty array in hooks for " . $event . "\n");
70 } else if (is_object($hook[0])) {
71 $object =& $wgHooks[$event][$index][0];
72 if (count($hook) < 2) {
73 $method = "on" . $event;
76 if (count($hook) > 2) {
81 } else if (is_string($hook[0])) {
83 if (count($hook) > 1) {
88 wfDebugDieBacktrace("Unknown datatype in hooks for " . $event . "\n");
90 } else if (is_string($hook)) { # functions look like strings, too
92 } else if (is_object($hook)) {
93 $object =& $wgHooks[$event][$index];
94 $method = "on" . $event;
96 wfDebugDieBacktrace("Unknown datatype in hooks for " . $event . "\n");
99 /* We put the first data element on, if needed. */
102 $hook_args = array_merge(array($data), $args);
108 if ( isset( $object ) ) {
109 $func = get_class( $object ) . '::' . $method;
113 wfProfileIn( $func );
114 if( isset( $object ) ) {
115 $retval = call_user_func_array(array(&$object, $method), $hook_args);
117 $retval = call_user_func_array($func, $hook_args);
119 wfProfileOut( $func );
121 /* String return is an error; false return means stop processing. */
123 if (is_string($retval)) {
125 $wgOut->fatalError($retval);
126 wfProfileOut( $fname );
128 } else if (!$retval) {
129 wfProfileOut( $fname );
134 wfProfileOut( $fname );